﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GabageFactory.Models;

namespace GabageFactory.Controllers
{
    [Authorize]
    public class FactoryController : BaseController
    {
        private FactoryReposity factoryReposity;

        private StackRainfallReposity stackRainfallReposity;

        private MaterialReposity materialReposity;
        
        public FactoryController()
        {
            factoryReposity = new FactoryReposity();
        }
        
        //
        // GET: /Factory/

        public ActionResult Index(string name)
        {
            ActionResult result = AllowForRole(RoleEnum.Administrator);
            if (result != null)
            {
                return result;
            }
            return View(factoryReposity.QueryFactories(name));
        }

        //
        // GET: /Factory/Details/5

        public ActionResult Details(int id)
        {
            ActionResult result = AllowForRole(RoleEnum.Administrator);
            if (result != null)
            {
                return result;
            }
            Factory factory = factoryReposity.GetFactoryById(id);
            return View(factory);
        }

        //
        // GET: /Factory/Create

        public ActionResult Create()
        {
            ActionResult result = AllowForRole(RoleEnum.Administrator);
            if (result != null)
            {
                return result;
            }
            return View(new Factory());
        } 

        //
        // POST: /Factory/Create

        [HttpPost]
        public ActionResult Create(Factory factory)
        {
            ActionResult result = AllowForRole(RoleEnum.Administrator);
            if (result != null)
            {
                return result;
            }
            try
            {
                factory = factoryReposity.CreateFactory(factory);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Factory/Edit/5
 
        public ActionResult Edit(int id)
        {
            ActionResult result = AllowForRole(RoleEnum.Administrator);
            if (result != null)
            {
                return result;
            }
            Factory factory = factoryReposity.GetFactoryById(id);
            ViewData["id"] = id;
            return View(factory);
        }

        //
        // POST: /Factory/Edit/5

        [HttpPost]
        public ActionResult Edit(Factory factory)
        {
            ActionResult result = AllowForRole(RoleEnum.Administrator);
            if (result != null)
            {
                return result;
            }
            try
            {
                ViewData["id"] = factory.Id;
                validate(factory);

                Factory oldFactory = factoryReposity.GetFactoryById(factory.Id);
                oldFactory.Name = factory.Name;
                oldFactory.Description = factory.Description;
                oldFactory.ModifyTime = DateTime.Now;
                factoryReposity.EditFactory(oldFactory);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Factory/Delete/5
 
        public ActionResult Delete(int id)
        {
            ActionResult result = AllowForRole(RoleEnum.Administrator);
            if (result != null)
            {
                return result;
            }
            Factory factory = factoryReposity.GetFactoryById(id);
            ViewData["Name"] = factory.Name;

            if (factory.Users.Count <= 0 && factory.Stacks.Count <= 0 && factory.Tailingponds.Count <= 0)
            {
                ViewData["CanDelete"] = true;
                factoryReposity.DeleteFactory(factory);
            }
            else
            {
                ViewData["CanDelete"] = false;
            }
            return View();
        }

        //
        // POST: /Factory/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            ActionResult result = AllowForRole(RoleEnum.Administrator);
            if (result != null)
            {
                return result;
            }
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        private void validate(Factory factory)
        {
            if (string.IsNullOrEmpty(factory.Name))
            {
                ModelState.AddModelError("Name", "堆场名称不能为空");
            }
            else if (factory.Name.Length > 128)
            {
                ModelState.AddModelError("Name", "堆场名称不能超过128个字符");
            }

            if (string.IsNullOrEmpty(factory.Description))
            {
                ModelState.AddModelError("Description", "堆场描述不能为空");
            }
            else if (factory.Description.Length > 2000)
            {
                ModelState.AddModelError("Description", "堆场名称不能超过2000个字符");
            }

            if (!ModelState.IsValid)
            {
                throw new Exception("Input form is invalid!");
            }
        }

        private int parseInt(string value)
        {
            int result = 0;
            try
            {
                result = int.Parse(value);
            }
            catch
            {
                // empty
            }

            return result;
        }

        private float parseFloat(string value)
        {
            float result = 0;
            try
            {
                result = float.Parse(value);
            }
            catch
            {
                // empty
            }

            return result;
        }
    }
}
